<?php
/**
 * Created by littleU.
 * User: littleU
 * Date: 2021-02-02
 * Time: 9:54
 */

namespace app\api\service;
use app\api\model\Admin as AdminModel;
use app\api\model\AdminMenu as AdminMenuModel;
use app\common\tool\IAuth;
use app\lib\exception\AdminException;


class Admin extends BaseService
{
    public static function getMenuByUid($uid)
    {
        $admin = (new AdminModel())->with([
            'roleMenu' => function($query){
                $query->with('menu');
            }
        ])->where('id',$uid)->find();
        $menuCollection = $admin->role_menu;
        $obj = new AdminMenuModel();
        $data = $obj->getTree($menuCollection);

        return json([
            'code' => config('code.success'),
            'data' => $data
        ]);
    }

    public static function getBasicInfoByAdmin($uid)
    {
        $admin = AdminModel::get($uid);
        $data = $admin->visible(['id','username','nickname','email','mobile','real_name','role_id'])->toArray();
        return json([
            'code' => config('code.success'),
            'data' => $data
        ]);
    }

    public static function setBasicInfoByAdmin($data)
    {
        $id = $data['id'];
        if($id == '')
        {
            try
            {
                $obj = new AdminModel($data);
                $obj->allowField(true)->save();
            }
            catch (\Exception $ex)
            {
                return json([
                    'code' => config('code.error'),
                    'msg'  => $ex->getMessage()
                ]);
            }

        }
        else
        {
            try
            {
                $obj = new AdminModel();
                $obj->allowField(true)->save($data, ['id' => $id]);
            }
            catch (\Exception $ex)
            {
                return json([
                    'code' => config('code.error'),
                    'msg'  => $ex->getMessage()
                ]);
            }
        }

        return json([
            'code' => config('code.success'),
            'msg'  => '操作成功'
        ]);
    }

    public static function setPasswordByAdmin($data, $uid)
    {
        $oldPassword = $data['oldPassword'];
        $admin = (new AdminModel())->where('id',$uid)->where('password',IAuth::setPassword($oldPassword))->find();
        if(!$admin)
        {
            throw new AdminException([
                'msg' => '原密码错误'
            ]);
        }
        try
        {
            $admin->password = IAuth::setPassword($data['password']);
            $admin->save();
        }
        catch (\Exception $ex)
        {
            return json([
                'code' => config('code.error'),
                'msg'  => $ex->getMessage()
            ]);
        }
        return json([
            'code' => config('code.success'),
            'msg'  => '操作成功'
        ]);
    }

    public static function setDefaultPassword($data)
    {
        try
        {
            $obj = AdminModel::get($data['id']);
            $obj->password = IAuth::setPassword(config('secure.default_password'));
            $obj->save();
        }
        catch (\Exception $ex)
        {
            return json([
                'code' => config('code.error'),
                'msg'  => $ex->getMessage()
            ]);
        }
        return json([
            'code'=> config('code.success'),
            'msg' => '初始密码为:'.config('secure.default_password')
        ]);

    }

    public function getAdminByCondition($data)
    {
        $condition = [];
        $page = isset($data['page']) ? $data['page'] : $this->page;
        $limit = isset($data['limit']) ? $data['limit'] : $this->limit;
        $order = isset($data['order']) ? $data['order'] : $this->order;
        if (isset($data['status'])) {
            $condition['status'] = $data['status'];
        }
        if (isset($data['name'])) {
            $condition['name'] = ['like', '%' . $data['name'] . '%'];
        }
        if (isset($data['code'])) {
            $condition['code'] = $data['code'];
        }
        $result = (new AdminModel())->with('role')->where($condition)->page($page)->limit($limit)->order($order)->select();
        $count = (new AdminModel())->where($condition)->count();

        return json([
            'code' => config('code.success'),
            'msg' => '请求成功',
            'data' => $result,
            'count' => $count
        ]);
    }
}